import 'package:flutter/material.dart';
import 'package:net/core/hi_base_tab_state.dart';
import 'package:net/http/dao/favorite_list_dao.dart';
import 'package:net/model/favorites_mo.dart';
import 'package:net/model/video_model.dart';
import 'package:net/navigator/hi_navigator.dart';
import 'package:net/page/video_detail.dart';
import 'package:net/utils/view_util.dart';
import 'package:net/widget/navigation_bar.dart';
import 'package:net/widget/video_large_card.dart';

///收藏
class FavoritePage extends StatefulWidget {
  @override
  _FavoritePageState createState() => _FavoritePageState();
}

class _FavoritePageState
    extends HBaseTabState<FavoriteMo, VideoModel, FavoritePage> {
  // 从详情页面回来刷新页面
  RouteChangeListener listener;
  @override
  void initState() {
    super.initState();
    HiNavigator.geInstance().addListener(this.listener = (current, pre) => {
          if (pre?.page is VideoDetailPage && current.page is FavoritePage)
            {loadData()}
        });
  }

  @override
  void dispose() {
    HiNavigator.geInstance().removeListener(this.listener);
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [_buildNavigationBar(), Expanded(child: super.build(context))],
    );
  }

  @override
  get contentChild => ListView.builder(
        padding: EdgeInsets.only(top: 10),
        itemCount: dataList.length,
        controller: scrollController,
        physics: const AlwaysScrollableScrollPhysics(),
        itemBuilder: (BuildContext context, int index) =>
            VideoLargeCard(videoModel: dataList[index]),
      );

  @override
  Future<FavoriteMo> getData(int pageIndex) async {
    FavoriteMo result = await FavoriteListDao.get();
    return result;
  }

  @override
  List<VideoModel> parseList(FavoriteMo resulet) {
    return resulet.list;
  }

  @override
  bool get wantKeepAlive => true;

  _buildNavigationBar() {
    return HNavigationBar(
      child: Container(
        decoration: bottomBoxShadow(context),
        alignment: Alignment.center,
        child: Text('收藏', style: TextStyle(fontSize: 16)),
      ),
    );
  }
}
